﻿$(function () {
    //点击链接弹出窗口
    $("input[type='button'].addUpdateItem:not([disabled])").live("click", function () {
        cleanForm();
        $("div.popupWindow").show("slow");
        if (this.name == "update") {
            $("h2:eq(0)", $("div.popupWindow")).attr("id", "updateItemTitle").html("更新申请信息");
            $("input#itemID").attr("readonly", true);
            var $parent = $(this).parent().parent();
            $("input#itemID").val($.trim($("td:eq(1)", $parent).text()));
            $("input#itemName").val($.trim($("td:eq(2)", $parent).text()));
            $("input#itemNumber").val($.trim($("td:eq(3)", $parent).text()));
            $("#itemDescription").val($.trim($("td:eq(4)", $parent).text()));
        } else {
            $("h2:eq(0)", $("div.popupWindow")).attr("id", "newAddItemTitle").html("添加新的申请");
            $("input#itemID").removeAttr("readonly");
            $("input#itemID").val("");
        }
        greyBg();
    });
    //提交表单
    $("input#btnSubmit").click(function () {
        if ($("input#itemName").val() == "") {
            $("span", $("input#itemName").parent()).text("物品名称不能为空！");
            return false;
        }
        var NoValue = $("input#itemNumber").val();
        if (NoValue == "") {
            $("span", $("input#itemNumber").parent()).text("申请数量不能为空！");
            return false;
        }
        var NoReg = /^\+?[1-9][0-9]*$/; //非零正整数
        if (!NoReg.test(NoValue)) {            
            $("span", $("input#itemNumber").parent()).text("只能填写数字！");
            return false;
        }
        var requestUrl = "../../myrequest/submit";
        if ($("h2:eq(0)", $("div.popupWindow")).attr("id") == "updateItemTitle") {
            requestUrl = "../../myrequest/update";
        }
        $.ajax({
            url: requestUrl,
            type: "POST",
            data: { "ID": $("input#itemID").val(), "productName": $("input#itemName").val(), "number": $("input#itemNumber").val(), "description": $("#itemDescription").val() },
            dataType: "json",
            success: function (data) {
                switch (data.status) {
                    case 1:
                        $("span", $("input#itemID").parent()).text("申请编号已经存在！");
                        break;
                    case 2:
                        $("div.popupWindow").hide();
                        $("#greybackground").hide();
                        alert("操作成功！");
                        window.location.reload();
                        //                                $("<tr><td>" + $("input#assetID").val() + "</td><td>" + $("input#assetModel").val() + "</td><td>" + $("select#assetType option:selected").text() + "</td><td>" + $("input#assetSn").val() + "</td><td>" + $("select#assetBelongsTo option:selected").text() + "</td><td>" + $("#assetDescription").val() + "</td><td><input class=\"addUpdateAsset\" id=\"btnUpdate\" type=\"button\" name=\"update\" value=\"修改\" /></td></tr>").insertBefore($("#tblMain tr:eq(1)"));
                        break;
                    case 4:
                    default:
                        alert("出错啦，联系管理员！");
                        break;
                }
            }
        });
    });
    //清空表单
    $("input#btnReset").click(function () {
        cleanForm();
    });
    //清空回复表单
    $("input#btnResetCom").click(function () {
        cleanFormCom();
    });
    //删除选中申请
    $("input#btnDelete").click(function () {
        if ($("input[type='checkbox']:checked", $("#tblMain tr td")).length == 0) {
            alert("请选择需要删除的申请");
            return;
        }
        if (confirm("确定删除选中的" + $("input[type='checkbox']:checked", $("#tblMain tr td")).length + "项申请?")) {
            var queryData = "";
            var ids = $("input[type='checkbox']:checked", $("#tblMain tr td"));
            $.each(ids, function (i, item) {
                if (i == 0) {
                    queryData = item.id;
                } else {
                    queryData += '||' + item.id;
                }
            });
            $.ajax({
                url: "../../myrequest/delete",
                type: "POST",
                data: { "ids": queryData },
                dataType: "json",
                success: function (data) {
                    switch (data.status) {
                        case 2:
                            alert("操作成功！");
                            window.location.reload();
                            break;
                        case 4:
                        default:
                            alert("出错啦，联系管理员！");
                            break;
                    }
                }
            });
        }
    });
    //点击审核回复选中的申请
    $("input[type='button'].recPassRejectItem").live("click", function () {
        if ($("input[type='checkbox']:checked", $("#tblMain tr td")).length == 0) {
            alert("请选择需要处理的申请");
            return;
        }
        if (confirm("确定选中的" + $("input[type='checkbox']:checked", $("#tblMain tr td")).length + "项申请?")) {
            cleanFormCom();
            $("div.popupWindow").show("slow");
            if (this.name == "receive") {
                $("h2:eq(0)", $("div.popupWindow")).attr("id", "receiveItemTitle").html("填写受理申请理由");
            }
            else if (this.name == "pass") {
                $("h2:eq(0)", $("div.popupWindow")).attr("id", "passItemTitle").html("填写通过申请理由");
            }
            else $("h2:eq(0)", $("div.popupWindow")).attr("id", "rejectItemTitle").html("填写拒绝申请理由");
            greyBg();
        }
    });
    //提交受理申请表单
    $("input#btnSubmitCom").click(function () {
        var queryData = "";
        var ids = $("input[type='checkbox']:checked", $("#tblMain tr td"));
        $.each(ids, function (i, item) {
            if (i == 0) {
                queryData = item.id;
            } else {
                queryData += '||' + item.id;
            }
        });
        var requestUrl = "../../requestmanage/receive";
        if ($("h2:eq(0)", $("div.popupWindow")).attr("id") == "passItemTitle") {
            requestUrl = "../../requestmanage/pass";
        }
        if ($("h2:eq(0)", $("div.popupWindow")).attr("id") == "rejectItemTitle") {
            requestUrl = "../../requestmanage/reject";
        }
        $.ajax({
            url: requestUrl,
            type: "POST",
            data: { "ids": queryData, "comments": $("#itemComments").val() },
            dataType: "json",
            success: function (data) {
                switch (data.status) {
                    case 2:
                        alert("操作成功！");
                        window.location.reload();
                        break;
                    case 4:
                    default:
                        alert("出错啦，联系管理员！");
                        break;
                }
            }
        });
    });

    function cleanForm() {
        if ($("input#itemID").attr("readonly") != true) $("input#itemID").val("");
        $("input#itemName").val("");
        $("input#itemNumber").val("");
        $("#itemDescription").val("");
        $("span", $("input#itemID").parent()).text(" *");
    }
    function cleanFormCom() {
        $("#itemComments").val("");
    }
    function greyBg() {
        $("#itemID").focus();
        //获取页面文档的高度
        var docheight = $(document).height();
        //追加一个层，使背景变灰
        $("#greybackground").css({ "opacity": "0.5", "height": docheight });
        $("#greybackground").show();
        return false;
    }

});